package studychinese.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.activity.BaseActivity;
import com.hschinese.pronunciation.interfaces.LessonProgressInterface;
import com.hschinese.pronunciation.interfaces.NextLessonInterface;
import com.hschinese.pronunciation.interfaces.RecordStatusListener;
import com.hschinese.pronunciation.interfaces.VolumeListener;
import com.hschinese.pronunciation.utils.NumberFormatUtil;
import com.hschinese.pronunciation.widget.BaseWordView;
import com.hschinese.pronunciation.widget.GestureListener;
import com.jaeger.library.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.hschinese.pronunciation.R.id.study_progress;


public class BaseWordShowActivity extends BaseActivity {
    @Bind(R.id.study_quit)
    ImageView studyQuit;
    @Bind(study_progress)
    ProgressBar studyProgress;
    @Bind(R.id.study_progress_layout)
    LinearLayout studyProgressLayout;
    @Bind(R.id.tv_process)
    TextView tvProcess;
    @Bind(R.id.study_main_view)
    LinearLayout studyMainView;
    @Bind(R.id.basestrokesview)
    BaseWordView basestrokesview;


    // 0第一个页面
    private int mCurrentPosition = 0;
    private NextLessonInterface mNextListener;
    private MainGestrueListener mGestureListener;
    private boolean isStarted = true;
    private RecordStatusListener mRecordListener;
    private VolumeListener mVolumeListener;
    //    private MyPopupWindow mPopupWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_word);
        ButterKnife.bind(this);
        Intent it = getIntent();
        // 启动出决定开始的是哪个课程
        mCurrentPosition = it.getIntExtra("position", 0);
        setNextListener();
        mGestureListener = new MainGestrueListener(BaseWordShowActivity.this);
        initViews();
        isStarted = false;
    }
    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, studyMainView);
    }
    private void initViews() {
        studyMainView.setLongClickable(true);
        studyMainView.setOnTouchListener(mGestureListener);
        basestrokesview.setNextListener(mNextListener);
        basestrokesview.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                studyProgress.setMax(total);
                studyProgress.setProgress(position + 1);
                tvProcess.setText(NumberFormatUtil.getPercent(position, total) + "%");
            }
        });
        basestrokesview.setData(mCurrentPosition);
    }
    private void setNextListener() {
        mNextListener = new NextLessonInterface() {
            @Override
            public void onLessonChanged(int currentLesson) {
                mCurrentPosition = currentLesson;
            }
        };
    }


    @Override
    public void onBackPressed() {
        this.finish();
    }

    private void gotoNextWord() {
       if (mCurrentPosition>=7){
           //弹出提示，去到下个模块
           showAlert();
       }else{
           basestrokesview.nextWord();
       }
    }
    private void showAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.next_lesson_dialog_msg);
        builder.setPositiveButton(R.string.btn_confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                startActivity(new Intent(BaseWordShowActivity.this,ComplexWordActivity.class).putExtra("position",0));
                BaseWordShowActivity.this.finish();
            }
        });
        builder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                BaseWordShowActivity.this.finish();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }


    @OnClick({R.id.study_quit, R.id.tv_process})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.study_quit:
                this.finish();
                break;
            case R.id.tv_process:
                break;
        }
    }


    class MainGestrueListener extends GestureListener {

        public MainGestrueListener(Context context) {
            super(context);
        }

        @Override
        public boolean left() {
            gotoNextWord();
            return true;
        }

        @Override
        public boolean right() {
            basestrokesview.beforeWord();
            return true;
        }
    }


}
